package cn.data.stream.config;

import cn.data.stream.config.parse.DataStreamConfigInfo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;

/**
 * @author shi hong yu
 */
@Configuration
@Slf4j
public class DataStreamConfig {
    @Value("${data.stream.config:}")
    private String config;
    @Bean
    public DataStreamConfigInfo getDataStreamConfig() { try {
        if(StringUtils.isBlank(config)){
            return null;
        }
        JAXBContext context = JAXBContext.newInstance(DataStreamConfigInfo.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        File file = ResourceUtils.getFile(config);
        if(!file.exists()){
            return null;
        }
        return (DataStreamConfigInfo) unmarshaller.unmarshal(file);
    } catch (Exception e) {
        log.warn("data-stream 加载失败:{} {}",config,e.getMessage());
        return null;
    }
    }
}
